/* web 本地存储相关函数封装 */

export const getItem = name => {
  const data = window.localStorage.getItem(name)
  try {
    // 当读取为空时, 不能转换为 json
    return JSON.parse(data)
  } catch (err) {
    return data
  }
}

export const setItem = (name, val) => {
  // 只有是对象的时候,才有必要转换为 JSON
  if (typeof val === 'object')
    val = JSON.stringify(val)
  window.localStorage.setItem(name, val)
}

export const removeItem = name => {
  window.localStorage.removeItem(name)
}